﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
// DNN
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common;
using DotNetNuke.Services.Exceptions;
// Codex
using CodexOmega.Modules.VideoLinker;

namespace CodexOmega.Modules.VideoLinker
{
    public partial class ManageAttributes : PortalModuleBase
    {
        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
                SetGridToShow();
            }
        }

        protected void linkAdd_Click(object sender, EventArgs e)
        {
            CreateNewProperty();
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void gridTypes_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string typeID = ((Label)((GridView)sender).Rows[e.RowIndex].FindControl("lblTypeID")).Text;
            string typeName = ((TextBox)((GridView)sender).Rows[e.RowIndex].FindControl("txtTypeName")).Text;

            UpdateVideoType(Convert.ToInt32(typeID), typeName);

            gridTypes.EditIndex = -1;
            BindGridTypes();
        }

        protected void gridTypes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string typeID = ((Label)((GridView)sender).Rows[e.RowIndex].FindControl("lblTypeID")).Text;

            DeleteVideoType(Convert.ToInt32(typeID));
        }

        protected void gridTypes_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gridTypes.EditIndex = e.NewEditIndex;
            BindGridTypes();
        }

        protected void gridTypes_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridTypes.EditIndex = -1;
            BindGridTypes();
        }

        protected void gridCategories_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string categoryID = ((Label)((GridView)sender).Rows[e.RowIndex].FindControl("lblCategoryID")).Text;
            string categoryName = ((TextBox)((GridView)sender).Rows[e.RowIndex].FindControl("txtCategoryName")).Text;

            UpdateVideoCategory(Convert.ToInt32(categoryID), categoryName);

            gridCategories.EditIndex = -1;
            BindGridCategories();
        }

        protected void gridCategories_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string categoryID = ((Label)((GridView)sender).Rows[e.RowIndex].FindControl("lblCategoryID")).Text;

            DeleteVideoCategory(Convert.ToInt32(categoryID));
        }

        protected void gridCategories_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gridCategories.EditIndex = e.NewEditIndex;
            BindGridCategories();
        }

        protected void gridCategories_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridCategories.EditIndex = -1;
            BindGridCategories();
        }

        protected void radioAttributesChoice_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetGridToShow();
        }

        #endregion

        #region Methods

        private void BindData()
        {
            BindGridCategories();
            BindGridTypes();
        }

        private void BindGridCategories()
        {
            // get controller
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            // bind grid categories
            gridCategories.DataSource = videoLinkerController.GetVideoCategoriesList();
            gridCategories.DataBind();
        }

        private void BindGridTypes()
        {
            // get controller
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            // bind grid types
            gridTypes.DataSource = videoLinkerController.GetVideoTypesList();
            gridTypes.DataBind();

        }

        private void CreateNewProperty()
        {
            // get controller
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            switch (radioRelation.SelectedItem.Value.ToLower())
            {
                case "category":
                    videoLinkerController.CreateVideoCategory(txtDefintion.Text.Trim());
                    break;
                case "type":
                    videoLinkerController.CreateVideoType(txtDefintion.Text.Trim());
                    break;
                default:
                    break;
            }

            // refresh the page
            Response.Redirect(Request.Url.AbsoluteUri);
        }

        private void UpdateVideoType(int typeID, string typeName)
        {
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            videoLinkerController.UpdateVideoType(typeID, typeName);
        }

        private void DeleteVideoType(int typeID)
        {
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            videoLinkerController.DeleteVideoType(typeID);
        }

        private void UpdateVideoCategory(int categoryID, string categoryName)
        {
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            videoLinkerController.UpdateVideoCategory(categoryID, categoryName);
        }

        private void DeleteVideoCategory(int categoryID)
        {
            VideoLinkerController videoLinkerController = new VideoLinkerController();

            videoLinkerController.DeleteVideoCategory(categoryID);

        }

        private void SetGridToShow()
        {
            panelCategories.Visible = (radioAttributesChoice.SelectedItem.Value.ToLower() == "categories");
            panelTypes.Visible = (radioAttributesChoice.SelectedItem.Value.ToLower() == "types");
        }

        #endregion
    }
}
